<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta name="author" content="ykf">
    <title>student练习</title>
    <style>
        
    </style>
</head>
<body>
    
</body>
</html>
<script>
    function student(name,isMale,birthdate){
        this.name = name;
        this.isMale = isMale;
        this.birthdate = birthdate;
    }

    student.prototype.eat = function(food){
        console.log(this.name+'吃了'+food);
    }
    student.prototype.sleep = function(hours){
        console.log(this.name+'睡了'+hours+'个小时');
    }
    student.prototype.play = function(game){
        console.log(this.name+'玩了'+game);
    }
    student.prototype.study = function(course){
        console.log(this.name+'学习了'+course);
    }

    var tom = new student('Tom','true','1995');

    Object.defineProperty(tom,'age',{
        set:function(val){
            if(val<0){

            return;
        }
        var today =new Date();
        this.birthdate = today.getFullYear() - val;
    },
    get:function(){
        var today = new Date();
        return today.getFullYear() - this.birthdate;
    }
         
    })
    var age = tom.age;
    tom.eat('香蕉');
    tom.sleep('16');
    tom.play('捉迷藏');
    tom.study('语文');

    console.log('age='+age);
    console.log(tom.name);
    console.log(tom.isMale);
    console.log(tom.birthdate);

</script>